package com.syni.mdd.sjb.common.datatreaure.listen.appNews;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserNewsSwitch;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserNewsSwitchService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
//@Component
@Log4j2
public class AppNewsSwitchListener implements ApplicationListener<AppNewsSwitchEvent> {

    @Autowired
    DxshAppUserNewsSwitchService dxshAppUserNewsSwitchService;


    @Override
    public void onApplicationEvent(AppNewsSwitchEvent appNewsSwitchEvent) {
        Integer userId = appNewsSwitchEvent.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches = dxshAppUserNewsSwitchService.list(queryWrapper);
        if(!CollectionUtils.isNotEmpty(dxshAppUserNewsSwitches)){
            List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitchesList = new ArrayList<>();
            Integer[] switchs = new Integer[]{Constants.APP_NEWS_MINAPP,Constants.APP_NEWS_MIDONGDONG,Constants.APP_NEWS_MT_SMART,Constants.APP_NEWS_SYSTEM};
            for(Integer parentType : switchs) {
                DxshAppUserNewsSwitch dxshAppUserNewsSwitch = new DxshAppUserNewsSwitch();
                dxshAppUserNewsSwitch.setIsOpen(Constants.COMMON_FLAG_TRUE_INT);
                dxshAppUserNewsSwitch.setNewTime(LocalDateTime.now());
                dxshAppUserNewsSwitch.setParentType(parentType);
                dxshAppUserNewsSwitch.setUserId(userId);
                dxshAppUserNewsSwitchesList.add(dxshAppUserNewsSwitch);
            }
            if(dxshAppUserNewsSwitchesList.size() > 0){
                dxshAppUserNewsSwitchService.saveBatch(dxshAppUserNewsSwitchesList);
            }
        }
    }
}
